cmake_minimum_required(VERSION 3.5.1)

# translator_qt5 _qmvar [... *.ts]
macro(translator_qt5 _qmvar)
    # set(SPARK_TRANSLATIONS_ARGN ${ARGN})
    # file(GLOB SPARK_TRANSLATIONS ${SPARK_TRANSLATIONS_ARGN})
    # qt5_add_translation(SPARK_QM_TRANSLATIONS 
        # ${SPARK_TRANSLATIONS})
    
    set(${_qmvar}_ARNG ${ARGN})
    file(GLOB ${_qmvar}_TS_FILES ${${_qmvar}_ARNG})
    
    find_package(Qt5LinguistTools)
    qt5_add_translation(${_qmvar}
        ${${_qmvar}_TS_FILES})

    set(SPARK_QM_TRANSLATIONS ${_qmvar})

    spark_debug_message("> QT Translation: ${_qmvar}")
    file(WRITE ${CMAKE_BINARY_DIR}/${_qmvar} "")
    foreach(item IN LISTS ${_qmvar})
        file(APPEND ${CMAKE_BINARY_DIR}/${_qmvar} "${item}\n")
        spark_debug_message("      ${item}")
    endforeach(item IN LISTS ${_qmvar})
    
    # 注意，必须将 SPARK_QM_TRANSLATIONS 或 ${_qmvar} 加入到 add_executable 参数中才能在编译时生成只有原文的ts文件
    
    # qt5_create_translation
        # ts文件会在 make clean 或重新编译的时候一并被删除，再编译的时候生成全新的ts（原有的翻译会丢失，万分注意！!）
    
    # qt5_add_translation
        # 此宏比较稳定
endmacro(translator_qt5 _qmvar)


# translator_qt6 _qmvar [... *.ts]
macro(translator_qt6 _qmvar)
    # todo
endmacro(translator_qt6 _qmvar)

# 冗余的 translator_qt5 或 qt6 的处理逻辑
macro(_handle_spark_translator_qt_macro _outvar)
    if(SPARK_FIND_QT5)
        translator_qt5(${_outvar} ${ARGN})
    endif(SPARK_FIND_QT5)

    if(SPARK_FIND_QT6)
        translator_qt6(${_outvar} ${ARGN})
    endif(SPARK_FIND_QT6)
endmacro(_handle_spark_translator_qt_macro _outvar)

# translator_qt _qmvar [... *.ts | match]
macro(translator_qt)
    _handle_spark_translator_qt_macro(${ARGN})
endmacro(translator_qt)


function(spark_translator_qt_ts ts_file scan_dir)

     # 1. 解析参数，使用原生 cmake 提供的参数解析器
    # set(OPTIONS)
    set(ONEVARG STORE)

    cmake_parse_arguments(QM "" "${ONEVARG}" "${MULVARG}" ${ARGN})

    # spark_debug_message("PATH ${QT_DIR}")
    # /usr/lib/x86_64-linux-gnu/cmake/Qt5
    # /usr/lib/x86_64-linux-gnu/qt5/bin/lupdate
    # /../../../bin  Windows Or Custome Qt
    # execute_process(COMMAND pwd) -> .../build

    if(SPARK_FIND_QT6)
        set(QT_LUPDATE ${QT_DIR}/../../qt6/bin/lupdate)
        set(QT_LRELEASE ${QT_DIR}/../../qt6/bin/lrelease)
        if(NOT EXISTS ${QT_LUPDATE})
            set(QT_LUPDATE ${QT_DIR}/../../bin/lupdate)
            set(QT_LRELEASE ${QT_DIR}/../../bin/lrelease)
            if(NOT EXISTS ${QT_LUPDATE})
                find_program(QT_LUPDATE NAMES lupdate)
                find_program(QT_LRELEASE NAMES lrelease)
                endif(NOT EXISTS ${QT_LUPDATE})
        endif(NOT EXISTS ${QT_LUPDATE})
    elseif(SPARK_FIND_QT5)
        set(QT_LUPDATE ${QT_DIR}/../../qt5/bin/lupdate)
        set(QT_LRELEASE ${QT_DIR}/../../qt5/bin/lrelease)
        if(NOT EXISTS ${QT_LUPDATE})
            set(QT_LUPDATE ${QT_DIR}/../../bin/lupdate)
            set(QT_LRELEASE ${QT_DIR}/../../bin/lrelease)
            if(NOT EXISTS ${QT_LUPDATE})
                find_program(QT_LUPDATE NAMES lupdate)
                find_program(QT_LRELEASE NAMES lrelease)
                endif(NOT EXISTS ${QT_LUPDATE})
        endif(NOT EXISTS ${QT_LUPDATE})
    else()
        find_program(QT_LUPDATE NAMES lupdate)
        find_program(QT_LRELEASE NAMES lrelease)
    endif(SPARK_FIND_QT6)

    spark_debug_message("[TS] ${CMAKE_CURRENT_LIST_DIR}")
    spark_debug_message("[TS] QT_LUPDATE:  ${QT_LUPDATE}")
    spark_debug_message("[TS] QT_LRELEASE: ${QT_LRELEASE}")

    spark_debug_message("[LUPDATE]: ${CMAKE_CURRENT_LIST_DIR}/${ts_file}")
    execute_process(COMMAND ${QT_LUPDATE}  ${scan_dir} -ts ${ts_file}
        WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
    execute_process(COMMAND ${QT_LRELEASE} ${ts_file}
        WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
    
    if(QM_STORE)
        if(NOT EXISTS ${QM_STORE})
            execute_process(COMMAND 
                ${CMAKE_COMMAND} -E make_directory "${QM_STORE}" 
                WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
        endif(NOT EXISTS ${QM_STORE})
        
        string(REPLACE ".ts" ".qm" qm_file ${ts_file})

        spark_debug_message("[LRELEASE]: ${CMAKE_CURRENT_LIST_DIR}/${ts_file}")
        spark_debug_message("         -> ${CMAKE_CURRENT_LIST_DIR}/${QM_STORE}")
        spark_debug_message("         -> ${QM_STORE}")
        execute_process(COMMAND 
            ${CMAKE_COMMAND} -E copy "${qm_file}" ${QM_STORE}
            WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
    endif(QM_STORE)
    
endfunction(spark_translator_qt_ts ts_file scan_dir)
